package com.example.bingobox.ui.todobox;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageButton;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.TodoBox;
import com.example.bingobox.model.Task;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class SimpleTaskAdapter extends RecyclerView.Adapter<SimpleTaskAdapter.ViewHolder> {
    private final List<TodoBox.TaskRef> tasks;
    private final Map<String, Task> taskMap;
    private final TodoBox todoBox;
    private final Runnable onDataChanged;

    public interface OnTaskActionListener {
        void onEdit(Task task);
    }

    private final OnTaskActionListener actionListener;

    public SimpleTaskAdapter(TodoBox todoBox, Map<String, Task> taskMap, 
                           Runnable onDataChanged, OnTaskActionListener actionListener) {
        this.todoBox = todoBox;
        this.tasks = todoBox.getListData().getList();
        this.taskMap = taskMap;
        this.onDataChanged = onDataChanged;
        this.actionListener = actionListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_task_simple, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TodoBox.TaskRef taskRef = tasks.get(position);
        Task task = taskMap.get(taskRef.getTaskId());
        
        if (task != null) {
            holder.nameText.setText(task.getName());
            if (task.getDeadline() > 0) {
                holder.deadlineText.setVisibility(View.VISIBLE);
                holder.deadlineText.setText(formatDate(task.getDeadline()));
            } else {
                holder.deadlineText.setVisibility(View.GONE);
            }

            holder.editButton.setOnClickListener(v -> {
                if (actionListener != null) {
                    actionListener.onEdit(task);
                }
            });

            holder.deleteButton.setOnClickListener(v -> {
                tasks.remove(position);
                notifyItemRemoved(position);
                onDataChanged.run();
            });
        }
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    private String formatDate(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd", Locale.getDefault());
        return sdf.format(new Date(timestamp));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView deadlineText;
        ImageButton editButton;
        ImageButton deleteButton;

        ViewHolder(View itemView) {
            super(itemView);
            nameText = itemView.findViewById(R.id.text_task_name);
            deadlineText = itemView.findViewById(R.id.text_task_deadline);
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }
} 